package com.sl.diary.adapter;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import com.sl.diary.R;
import com.sl.diary.model.TravelModel;
import com.sl.diary.util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TravelItemAdapter extends ArrayAdapter<TravelModel> {

	private final Context context;
	private List<TravelModel> travels = null;
	private TravelItemAdapterEventCallback callback;

	public TravelItemAdapter(Context context, int textViewResourceId,
			List<TravelModel> objects) {

		super(context, textViewResourceId, objects);
		this.context = context;
		this.travels = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.a003_travel_list_row, null);
		}

		/*
		 * set values for field in travel list row
		 */
		TravelModel travel = this.travels.get(position);
		String id = travel.getId();

		// set travel image
		ImageView img_travel = (ImageView) convertView
				.findViewById(R.id.img_travel_row_icon);
		URL url;
		try {
			url = new URL(travel.getIcon());
			Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection()
					.getInputStream());
			img_travel.setImageBitmap(bitmap);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// set travel name
		TextView tv_travelName = (TextView) convertView
				.findViewById(R.id.tv_travel_row_name);
		tv_travelName.setText(travel.getName());
		// set travel date
		TextView tv_travelDate = (TextView) convertView
				.findViewById(R.id.tv_travel_date);
		tv_travelDate.setText(this.getTravelDuration(
				travel.getDeparture_date(), travel.getEnd_date()));
		// set travel description
		TextView tv_travelDescription = (TextView) convertView
				.findViewById(R.id.tv_travel_row_description);
		tv_travelDescription.setText(travel.getDescription());

		// add event for icon
		ImageView img_travel_edit = (ImageView) convertView
				.findViewById(R.id.imgbtn_travel_row_edit);
		img_travel_edit.setTag(position);
		img_travel_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (callback != null) {
					callback.onEditTravel(view);
				}

			}
		});

		ImageView img_travel_delte = (ImageView) convertView
				.findViewById(R.id.imgbtn_travel_row_delete);
		img_travel_delte.setTag(position);
		img_travel_delte.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (callback != null) {
					callback.onDeleteTravel(view);
				}
			}
		});

		ImageView img_travel_arrow = (ImageView) convertView
				.findViewById(R.id.imgbtn_travel_row_arrow);
		img_travel_arrow.setTag(position);
		img_travel_arrow.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (callback != null) {
					callback.onShowTravelDetail(view);
				}
			}
		});

		return convertView;
	}

	/**
	 * Get travel duration string by format as 'From: dd/MM/yyyy To: dd/MM/yyyy'
	 * 
	 * @param departure_date
	 *            the date
	 * @param end_date
	 *            the date
	 * @return the string
	 */
	private String getTravelDuration(Date departure_date, Date end_date) {
		String duration = String.format("From: %s To: %s",
				Util.convertDateToString(departure_date),
				Util.convertDateToString(end_date));
		return duration;
	}

	/**
	 * Set interface for adapter
	 * 
	 * @param callback
	 *            the TravelItemAdapterEventCallback
	 */
	public void setCallback(TravelItemAdapterEventCallback callback) {

		this.callback = callback;
	}

	/**
	 * @interface TravelItemAdapterEventCallback - call back to activity to
	 *            handle click event on edit/delete image
	 */
	public interface TravelItemAdapterEventCallback {

		public void onEditTravel(View view);

		public void onDeleteTravel(View view);

		public void onShowTravelDetail(View view);
	}
}
